package com.alibaba.citrus.ots.sales.plan.facade.service.statistics;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.common.exception.FacadeException;
import com.alibaba.citrus.ots.sales.plan.api.statistics.SalesPlanCustomerStatQueryService;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.DimensionDTO;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.PageQueryPlanCustomerStatRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.SalesPlanStatCustomerSubTotalRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.response.SalesPlanScItemStatSubTotalResponse;
import com.alibaba.citrus.ots.sales.plan.facade.service.SalesPlanCommonService;
import com.alibaba.citrus.ots.sales.plan.model.enums.SalesStatBizTypeEnum;
import com.alibaba.citrus.ots.sales.plan.repository.SalesPlanCustomerStatRepository;
import com.alibaba.citrus.ots.sales.plan.repository.request.SalesPlanStatCustomerSubTotalRepoRequest;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanSDO;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanStatCustomerSDO;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.bosalesplancustomerindex.dto.PageQueryPlanCustomerRequest;
import com.epoch.app.bcots.bosalesplancustomerindex.dto.PageQueryPlanCustomerResponse;
import com.epoch.app.bcots.bosalesplancustomerindex.service.BoSalesPlanCustomerIndexService;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yuhailin
 * @description:
 * @date 2021-09-27 10:28
 */
@Component
public class SalesPlanCustomerStatQueryServiceImpl implements SalesPlanCustomerStatQueryService {
    @Resource
    private SalesPlanCustomerStatRepository salesPlanCustomerStatRepository;

    @Resource
    private SalesPlanCommonService salesPlanCommonService;

    @Resource
    private BoSalesPlanCustomerIndexService boSalesPlanCustomerIndexService;

    @FacadeInvoker
    @Override
    public Result<PageResult<List<SalesPlanStatCustomerSDO>>> pageQueryPlanCustomerList(PageQueryPlanCustomerStatRequest pageQueryPlanCustomerStatRequest) {
        SalesPlanSDO salesPlan = salesPlanCommonService.checkSalesPlan(pageQueryPlanCustomerStatRequest.getPlanId());
        DimensionDTO dimension = salesPlanCommonService.checkBizType(salesPlan, pageQueryPlanCustomerStatRequest);
        PageQueryPlanCustomerRequest planCustomerRequest = new PageQueryPlanCustomerRequest();
        if (StringUtils.isNotBlank(pageQueryPlanCustomerStatRequest.getPlanId())){
            planCustomerRequest.setPlanIdList(Lists.newArrayList(pageQueryPlanCustomerStatRequest.getPlanId()));
        }
        if (StringUtils.isNotBlank(pageQueryPlanCustomerStatRequest.getCustomerCode())){
            planCustomerRequest.setCustomerCodeList(Lists.newArrayList(pageQueryPlanCustomerStatRequest.getCustomerCode()));
        }
        if (StringUtils.isNotBlank(pageQueryPlanCustomerStatRequest.getManageEmployeeId())){
            planCustomerRequest.setManageEmployeeIdList(Lists.newArrayList(pageQueryPlanCustomerStatRequest.getManageEmployeeId()));
        }
        planCustomerRequest.setSubCustomerName(pageQueryPlanCustomerStatRequest.getCustomerName());
        planCustomerRequest.setSubCustomerAddressName(pageQueryPlanCustomerStatRequest.getCustomerAddressName());
        planCustomerRequest.setDir(planCustomerRequest.getDir());
        planCustomerRequest.setAsc(pageQueryPlanCustomerStatRequest.getAsc());
        planCustomerRequest.setStart(pageQueryPlanCustomerStatRequest.getStart());
        planCustomerRequest.setLimit(pageQueryPlanCustomerStatRequest.getLimit());
        planCustomerRequest.setStatus(pageQueryPlanCustomerStatRequest.getStatus());
        //查询调整数据
        if (pageQueryPlanCustomerStatRequest.getIsAdjustCount()) {
            planCustomerRequest.setAdjustCount(0);
        }
        SalesStatBizTypeEnum match = SalesStatBizTypeEnum.match(dimension.getBizType());
        switch (match){
            case SALESMAN:
                if (StringUtils.isNotBlank(pageQueryPlanCustomerStatRequest.getManageEmployeeId())){
                    if (!pageQueryPlanCustomerStatRequest.getManageEmployeeId().equals(dimension.getBizId())){
                        throw new FacadeException("OTS-02-001-00-15-0012");
                    }
                }else {
                    planCustomerRequest.setManageEmployeeIdList(Lists.newArrayList(dimension.getBizId()));
                }
                break;
            case SALES_DEPARTMENT:
                planCustomerRequest.setOrgSalesDepartmentCodeList(Lists.newArrayList(dimension.getBizId()));
                break;
            case SALES_CHANNEL:
                planCustomerRequest.setOrgSalesChannelCodeList(Lists.newArrayList(dimension.getBizId()));
                break;
            case SALES_ORGANIZATION:
                planCustomerRequest.setOrgSalesOrganizationCodeList(Lists.newArrayList(dimension.getBizId()));
                break;
            case BUSINESS_UNIT:
                planCustomerRequest.setOrgBusinessUnitCodeList(Lists.newArrayList(dimension.getBizId()));
                break;
            default:
                throw new FacadeException("OTS-02-002-02-15-007");
        }
        PageQueryPlanCustomerResponse pageQueryPlanCustomerResponse = boSalesPlanCustomerIndexService.pageQueryPlanCustomer(planCustomerRequest);
        if (pageQueryPlanCustomerResponse == null || CollectionUtils.isEmpty(pageQueryPlanCustomerResponse.getResult())){
            return Result.success(new PageResult<>(Lists.newArrayList(),0));
        }
        List<SalesPlanStatCustomerSDO> result = pageQueryPlanCustomerResponse.getResult().stream().map(boSalesPlanCustomerIndex -> {
            SalesPlanStatCustomerSDO customerSDO = new SalesPlanStatCustomerSDO();
            BeanUtils.copyProperties(boSalesPlanCustomerIndex,customerSDO);
            customerSDO.setCustomerName(boSalesPlanCustomerIndex.getSubCustomerName());
            customerSDO.setCustomerAddressName(boSalesPlanCustomerIndex.getSubCustomerAddressName());
            return customerSDO;
        }).collect(Collectors.toList());
        return Result.success(new PageResult<>(result,pageQueryPlanCustomerResponse.getTotal()));
    }

    @FacadeInvoker
    @Override
    public Result<SalesPlanScItemStatSubTotalResponse> subTotalSalesPlanStatCustomer(SalesPlanStatCustomerSubTotalRequest salesPlanStatCustomerSubTotalRequest) {
        SalesPlanSDO salesPlan = salesPlanCommonService.checkSalesPlan(salesPlanStatCustomerSubTotalRequest.getPlanId());
        DimensionDTO dimension = salesPlanCommonService.checkBizType(salesPlan, salesPlanStatCustomerSubTotalRequest);
        SalesStatBizTypeEnum match = SalesStatBizTypeEnum.match(dimension.getBizType());
        SalesPlanStatCustomerSubTotalRepoRequest repoRequest = new SalesPlanStatCustomerSubTotalRepoRequest();
        repoRequest.setPlanId(salesPlanStatCustomerSubTotalRequest.getPlanId());
        repoRequest.setCustomerCode(salesPlanStatCustomerSubTotalRequest.getCustomerCode());
        repoRequest.setEmployeeId(salesPlanStatCustomerSubTotalRequest.getEmployeeId());
        //查询调整数据
        if (salesPlanStatCustomerSubTotalRequest.getIsAdjustCount()) {
            repoRequest.setAdjustCount(0);
        }
        switch (match){
            case SALESMAN:
                if (StringUtils.isNotBlank(salesPlanStatCustomerSubTotalRequest.getEmployeeId())){
                    if (!salesPlanStatCustomerSubTotalRequest.getEmployeeId().equals(dimension.getBizId())){
                        throw new FacadeException("OTS-02-001-00-15-0012");
                    }
                }else {
                    repoRequest.setEmployeeId(dimension.getBizId());
                }
                break;
            case SALES_DEPARTMENT:
                repoRequest.setOrgSalesDepartmentCode(dimension.getBizId());
                break;
            case SALES_CHANNEL:
                repoRequest.setOrgSalesChannelCode(dimension.getBizId());
                break;
            case SALES_ORGANIZATION:
                repoRequest.setOrgSalesOrganizationCode(dimension.getBizId());
                break;
            case BUSINESS_UNIT:
                repoRequest.setOrgBusinessUnitCode(dimension.getBizId());
                break;
            default:
                throw new FacadeException("OTS-02-002-02-15-007");
        }

        return Result.success(salesPlanCustomerStatRepository.subTotalSalesPlanStatCustomer(repoRequest));
    }
}
